package com.thb.pay.payment.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.thb.pay.api.payment.PayProductServiceApi;
import com.thb.pay.api.payment.vo.req.PayProductPageReq;
import com.thb.pay.api.payment.vo.req.PayProductReq;
import com.thb.pay.api.payment.vo.resp.PayProductVo;
import com.thb.pay.core.exception.RRException;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.thb.pay.payment.entity.PayProduct;
import com.thb.pay.payment.service.PayProductService;
import com.xiaoleilu.hutool.bean.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 支付-产品表 前端控制器
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@RestController
public class PayProductController implements PayProductServiceApi {

    @Autowired
    private PayProductService payProductService;

    @Override
    public PageResult<PayProductVo> findPage(@RequestBody PayProductPageReq pageReq) {
        IPage<PayProduct> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<PayProduct> pageResult = payProductService.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, PayProductVo.class);
    }

    @Override
    public List<PayProductVo> findAllList() {
        List<PayProductVo> resultDataList = Lists.newArrayList();
        List<PayProduct> resDataList = payProductService.selectList(null);
        resDataList.forEach(payProduct -> {
            PayProductVo resultData = new PayProductVo();
            BeanUtil.copyProperties(payProduct, resultData);
            resultDataList.add(resultData);
        });
        return resultDataList;
    }

    public List<PayProductVo> findListByProductIds(@RequestBody List<Long> productIds) {
        List<PayProductVo> resultDataList = Lists.newArrayList();
        List<PayProduct> resDataList = payProductService.selectBatchIds(productIds);
        resDataList.forEach(payProduct -> {
            PayProductVo resultData = new PayProductVo();
            BeanUtil.copyProperties(payProduct, resultData);
            resultDataList.add(resultData);
        });
        return resultDataList;
    }

    @Override
    public PayProductVo getById(@RequestParam("id") Long id) {
        PayProductVo payProductVo = null;
        PayProduct payProduct = payProductService.selectById(id);
        if (payProduct != null) {
            payProductVo = new PayProductVo();
            BeanUtil.copyProperties(payProduct, payProductVo);
        }
        return payProductVo;
    }

    @Override
    public void add(@RequestBody PayProductReq payProductReq) {
        PayProduct payProduct = new PayProduct();
        BeanUtil.copyProperties(payProductReq, payProduct);
        payProductService.insert(payProduct);
    }

    @Override
    public void update(@RequestBody PayProductReq payProductReq) {
        PayProduct payProduct = payProductService.selectById(payProductReq.getId());
        if (payProduct == null) {
            throw RRException.build("pay.product.not.exist");
        }
        PayProduct targetPayProduct = new PayProduct();
        BeanUtil.copyProperties(payProductReq, targetPayProduct);
        payProductService.updateById(targetPayProduct);
    }
}
